package com.rs.controller;

import com.rs.entity.Laboratory;
import com.rs.entity.Response;
import com.rs.entity.Student;
import com.rs.service.ClassService;
import com.rs.service.LaboratoryService;
import com.rs.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * @author dehang
 * @date 2019/06/01
 */
@Controller
@RequestMapping("laboratory")
public class LaboratoryController {

    @Autowired
    private ClassService classService;
    @Autowired
    private LaboratoryService laboratoryService;

    @RequestMapping("/index")
    public String index(Model model) {
        return "laboratory/index";
    }

    @RequestMapping("/list")
    @ResponseBody
    public Response list(long page, long limit,String name) {
        List<Laboratory> classes = laboratoryService.getClassList(page,limit,name);
        return Response.list(classes,laboratoryService.getClassSum(name));
    }

    @RequestMapping("/add")
    @ResponseBody
    public long addClass(String name, String remark) {

        return laboratoryService.addClass(name, remark);
    }

    @RequestMapping("/update")
    @ResponseBody
    public void updateClass(Long id, String name, String remark) {

        laboratoryService.updateClass(id, name, remark);
    }

    @RequestMapping("/del")
    @ResponseBody
    public void delClass(long id) {

        laboratoryService.delClass(id);
    }

    @RequestMapping("/getTerms")
    @ResponseBody
    public Response<List<Laboratory>> getTerms() {
        List<Laboratory> terms=laboratoryService.list();
        return Response.success(terms);
    }

    @RequestMapping("/allocation")
    public String allocation(Model model, long id) {

        //已有学生
        List<Student> havestudents=classService.getHaveStudent(id);
        //所有学生
        List<Student> students=classService.getStudent();

        for (Student s:students){
            for (Student hs:havestudents){
                if(s.getId()==hs.getId()){
                    s.setChecked(true);
                    break;
                }
            }
        }
        model.addAttribute("two", students);
        model.addAttribute("id",id);
        return "class/allocation";
    }

    @RequestMapping("/allot")
    @ResponseBody
    public String allot(String mid,int id){
        classService.allot(mid,id);
        return "success";
    }

}
